package com.sd.repay.auth;

import com.sd.repay.auth.channel.BaseRealAuth;
import com.sd.repay.auth.channel.SHRealService;
import com.sd.repay.common.Constants;
import com.sd.repay.pojo.RealAuthBusi;
import com.sd.repay.pojo.RealAuthRecord;
import com.sd.repay.service.RealAuthService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


/**
 * @author
 */
@Service
public class RealAuthRoute {

    private Logger log = LoggerFactory.getLogger(RealAuthRoute.class);

    private static Map<String, Object> channelMap = new HashMap<>();

    @Autowired
    private RealAuthService realAuthService;


    @Resource
    private SHRealService shRealService;


    private void init() {
        channelMap.put("SH_REAL", shRealService);
    }


    /**
     * 获取鉴权通道
     *
     * @param defChannel 默认通道
     * @param realType   鉴权类型
     * @return
     */
    public String getRealAuthChannel(String defChannel, String realType) {
        RealAuthBusi realAuthBusi = null;
        try {
            if (StringUtils.isNotBlank(defChannel)) {
                realAuthBusi = realAuthService.getRealAuthBusi(realType, defChannel, "1");
            } else {
                realAuthBusi = realAuthService.getRealAuthBusi(realType, "1");
            }
        } catch (Exception e) {
            log.error("实名认证默认渠道[{}] 鉴权类型[{}], 未配置渠道业务", defChannel, realType);
            return "";
        }


        if (realAuthBusi == null) {
            return "";
        }

        return realAuthBusi.getRealChannel();
    }


    /**
     * @param channelCode
     * @return
     */
    private BaseRealAuth getRealAuth(String channelCode) {
        BaseRealAuth baseRealAuth = null;
        if (channelMap.isEmpty()) {
            init();
        }

        Object obj = channelMap.get(channelCode);

        if (obj != null) {
            try {
                baseRealAuth = (BaseRealAuth) obj;
            } catch (Exception e) {
                log.error("实名认证出现异常:{}", e.getMessage());
                return baseRealAuth;
            }

        }

        return baseRealAuth;
    }


    /**
     * 鉴权
     *
     * @param authRecord
     */
    public void realAuth(RealAuthRecord authRecord) {
        String channelCode = authRecord.getAuthChannel();
        String realType = authRecord.getAuthType();
        BaseRealAuth baseRealAuth = getRealAuth(channelCode);
        if (baseRealAuth == null) {
            authRecord.setAuthStatus("2");
            authRecord.setRespMsg(String.format("未知的渠道标识:%s", channelCode));
            return;
        }


        if (realType.equals(Constants.AUTH_TYPE_TWO)) {
            baseRealAuth.twoElementsCheck(authRecord);
        } else if (realType.equals(Constants.AUTH_TYPE_THREE)) {
            baseRealAuth.threeElementsCheck(authRecord);
        } else if (realType.equals(Constants.AUTH_TYPE_FOUR)) {
            baseRealAuth.fourElementsCheck(authRecord);
        }
    }

}
